package org.fas.container
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import org.foxas.DataObject.DaPos;
	import org.foxas.base.FV;
	import org.foxas.base.FFunc;
	import org.foxas.base.FMath;
	import org.foxas.tween.InertiaTween;
	import org.foxas.tween.TransTween;
	public class Tweener extends Sprite{
		public var runState:String = "";
		protected var mouseDownTime:Number;
		//now
		protected var mx0:Number = 0;//mouseX when move now
		protected var my0:Number = 0;
		protected var speedX:Number = 0;
		protected var speedY:Number = 0;
		//last
		protected var mx1:Number = 0;//mouseX when move last
		protected var my1:Number = 0;
		//
		//init
		public var inertiaTween:InertiaTween = new InertiaTween();
		public var transTweenX:TransTween = new TransTween();
		protected var transTweenY:TransTween = new TransTween();
		protected var transTweenR:TransTween = new TransTween();
		protected var transTweenW:TransTween = new TransTween();
		protected var transTweenH:TransTween = new TransTween();
		//
		protected var par_r:Object;//this.parent
		public function set par(_ob:Object):void{
			par_r=_ob;
			//par_r.addEventListener(MouseEvent.MOUSE_UP,mouseUpHandle);
			//par_r.addEventListener(MouseEvent.MOUSE_OUT,mouseOutHandle);
		}
		public function get par():Object{
			return par_r;
		}
		public var w:Number = 0;
		public var h:Number = 0;
//		public function set w(_v:Number):void{
//			w_r = 
//		}
//		public function get w():Number{
//			
//		}
		public var wOri:Number = 0;
		public var hOri:Number = 0;
		public function oriWH(_w:Number,_h:Number):void{
			wOri = _w;
			hOri = _h;
			w=_w;
			h=_h;
		}
		protected var isDown_r:Boolean = false;
		protected function set isDown(_bl:Boolean):void{
			isDown_r = _bl;
			if(_bl){//true
				//par.setChildIndex(this,par.numChildren-1); //make the displayObject to top of children
//				if(Config.dragMode==1){//normal drag
//					this.rotation = 0;
//					this.x = par.mouseX - this.width/2;
//					this.y = par.mouseY - this.height/2;
//				}
			//	this.overRim.visible = false;
			//	downRim.visible = true;
			//	this.downGlow.target = downRim;
			//	this.downGlow.play();
				partDataBakJello = LibFunc.getPartDataFromDrager(this).clone();
				this.addEventListener(Event.ENTER_FRAME,frameHandle);
			}else{//false
			//	downRim.visible = false;
				this.removeEventListener(Event.ENTER_FRAME,frameHandle);
			}
		}
		protected function get isDown():Boolean{
			return isDown_r;
		}
		public function Tweener(){
			init();
		}
		protected function init():void{
		//	par = this.parent;
//			this.addEventListener(MouseEvent.MOUSE_DOWN,mouseDownHandle);
//			this.addEventListener(MouseEvent.MOUSE_UP,mouseUpThisHandle);
		//	this.addEventListener(MouseEvent.CLICK,clickHandle);
		//	this.addEventListener(MouseEvent.DOUBLE_CLICK,doubleClickHandle);
			//this.addEventListener(MouseEvent.ROLL_OVER,mouseOverHandle);
			//this.addEventListener(MouseEvent.ROLL_OUT,mouseOutThisHandle);	
		}
		//handle
		protected function clickHandle(_e:MouseEvent=null):void{
		}
		public function mouseDownHandle(_e:MouseEvent=null):void{
			if(!inertiaTween.isRun){
				mouseDownTime = new Date().time;
				this.isDown = true;
				mx0 = par.mouseX;
				my0 = par.mouseY;
				this.runState = "drag";
			}
		}
		public function mouseUpThisHandle(_e:MouseEvent=null):void{
			mouseUpHandle(_e);
		}
		public function mouseUpHandle(_e:MouseEvent=null):void{
			if(isDown){
				var mouseUpTime:Number = new Date().time;
//				if((mouseUpTime-mouseDownTime)<130){//click handle
//					clickHandle(_e);
//				}else{ //if(Config.inertia==1){
					speedX = (speedX + par.mouseX - mx0)/2;
					speedY = (speedY + par.mouseY - my0)/2;
					inertiaTween.fix(par.mouseX,speedX,par.mouseY,speedY,par.width,par.height);
					inertiaTween.listener_frame = dragCount;
					inertiaTween.listener_complete = inertiaCompleteHandle;
					//inertiaTween.listener_out = outHandle;
					inertiaTween.start();
//				}
				this.isDown = false;
			}
		}
		protected function outHandle():void{
			par.outHandle(this);
		}
		protected function mouseOverHandle(_e:MouseEvent):void{
			if(!isDown){}
		}
		protected function mouseOutThisHandle(_e:MouseEvent):void{
		}
		protected function mouseOutHandle(_e:MouseEvent=null):void{//only for parent
			//this.isDown = false;
//			var _mx:Number = par.mouseX;
//			var _my:Number = par.mouseY;
//			if(_mx<20 || _mx>(par.width-20) || _my<20 || _my>(par.height-20)){//when mouse over card cann't handle this listen 
//				mouseUpHandle(_e);
//			}
		}
		//protected function mouseClickHandle(_e:MouseEvent):void{
		//	trace("click");	
		//}
		protected function frameHandle(_e:Event):void{
			dragCount(par.mouseX,par.mouseY);
		}
		public var selfSpeedX:Number=0;
		public var selfSpeedY:Number=0
		public function dragCount(_mouseX:Number,_mouseY:Number):void{
			mx1 = _mouseX;
			my1 = _mouseY;
			//
			var cx0:Number;
			var cy0:Number;
			var _r0:Number;
			var _r1:Number;
			var _dis:Number;
			//
//			if(Config.dragMode==2 || Config.dragMode==1){
	//			this.x = this.x-mx0+mx1;
		//		this.y = this.y-my0+my1;
			//}else if(Config.dragMode==3){
			
				cx0 = this.centerX;
				cy0 = this.centerY;
				_r0 = FMath.angle((mx0-cx0),(my0-cy0));
				_r1 = FMath.angle((mx1-cx0),(my1-cy0));
				_dis = FMath.dis((mx0-cx0),(my0-cy0));
				this.rotation += (_r1-_r0);
				this.centerX = mx1 - _dis*FMath.cos(_r1);
				this.centerY = my1 - _dis*FMath.sin(_r1);
//				*/
//				this.rotation = 45;
//				this.x += (mx1-mx0);
//				this.y += (my1-my0);
				//trace(this.rotation);
			//}
		//
			speedX = (speedX + mx1 - mx0)/2;//mouse x offset speed
			speedY = (speedY + my1 - my0)/2;
		//
			var _x:Number=0; 
			var _y:Number=0;
			var _daPos:DaPos;
			if(this.type!="case"){
				_x += speedX*FMath.cos(this.rotation);
				_y -= speedX*FMath.sin(this.rotation);
				_x += speedY*FMath.sin(this.rotation);
				_y += speedY*FMath.cos(this.rotation);
			//
//				selfSpeedX =(selfSpeedX*2+_x)/3;
//				selfSpeedY =(selfSpeedY*2+_y)/3;
				selfSpeedX = _x;
				selfSpeedY = _y;
				_daPos = FFunc.posParToChild(this,mx1,my1);
				this.jello(_daPos.x,_daPos.y,selfSpeedX,selfSpeedY);
			}
			//
			mx0 = mx1;
			my0 = my1;
		}
		protected function inertiaCompleteHandle():void{
			jelloStop();
			//this.centerX = FaMath.area(this.centerX,par.width-20,20);
			//this.centerY = FaMath.area(this.centerY,par.height-20,20);
		}
		/**
		 * get center position(x) across value of origin position(x)
		 */
		public function get centerX():Number{
			var _w:Number = this.w;
			var _h:Number = this.h;
			var _dis:Number = FMath.dis(_w,_h)/2;
			var _cr:Number = FMath.angle(_w,_h);
			return this.x + _dis*FMath.cos(this.rotation+_cr);
		}
		/**
		 * set origin position(x) across value of center position(x)
		 */
		public function set centerX(_v:Number):void{
			var _w:Number = this.w;
			var _h:Number = this.h;
			var _dis:Number = FMath.dis(_w,_h)/2;
			var _cr:Number = FMath.angle(_w,_h);
			this.x = _v - _dis*FMath.cos(this.rotation+_cr);
		}
		/**
		 * get center position(yY) across value of origin position(y)
		 */
		public function get centerY():Number{
			var _w:Number = this.w;
			var _h:Number = this.h;
			var _dis:Number = FMath.dis(_w,_h)/2;
			var _cr:Number = FMath.angle(_w,_h);
			return this.y + _dis*FMath.sin(this.rotation+_cr);
		}
		/**
		 * set origin position(y) across value of center position(y)
		 */
		public function set centerY(_v:Number):void{
			var _w:Number = this.w;
			var _h:Number = this.h;
			var _dis:Number = FMath.dis(_w,_h)/2;
			var _cr:Number = FMath.angle(_w,_h);
			this.y = _v - _dis*FMath.sin(this.rotation+_cr);
		}
		public function set scale(_v:Number):void{
//			trace(this.centerX,this.centerY,this.width,this.height,this.w,this.h);
			var _cx:Number = this.centerX;
			var _cy:Number = this.centerY;
			this.scaleX = _v;
			this.scaleY = _v;
			this.w = this.wOri*this.scaleX;
			this.h = this.hOri*this.scaleY;
			this.centerX = _cx;
			this.centerY = _cy;
		}
		public function get scale():Number{
			return this.scaleX;
		}
		private var lisComplete:Function;
		private function lisCompleteHandle(_v:Number):void{
			if(lisComplete!=null){
				lisComplete(this);
				lisComplete = null;
			}
		}
		//transTween
		public function initPosition(_cx:Number=FV.un,_cy:Number=FV.un,_r:Number=FV.un,_s:Number=FV.un,_listener_complete:Function=null,_lisHalf:Function=null,_time:uint=30):void{
			this.lisComplete = _listener_complete;
			this.visible = true;
			var isAddListener:Boolean = false;
			var _tweenType:String = "--";
			if(_cx!=FV.un){
				transTweenX.fix(this,"centerX",centerX,_cx,_time,_tweenType);	
				transTweenX.start();
				transTweenX.lisHalf = _lisHalf;
				transTweenX.listener_complete = lisCompleteHandle;
				isAddListener = true;
			}
			if(_cy!=FV.un){
				transTweenY.fix(this,"centerY",centerY,_cy,_time,_tweenType);	
				transTweenY.start();
				if(isAddListener==false){
					transTweenY.lisHalf = _lisHalf;
					transTweenX.listener_complete = null;
					transTweenY.listener_complete = lisCompleteHandle;
					isAddListener = true;
				}
			}
			if(_r!=FV.un){
				transTweenR.fix(this,"rotation",rotation,_r,_time,_tweenType);	
				transTweenR.start();
				if(isAddListener==false){
					transTweenR.lisHalf = _lisHalf;
					transTweenY.listener_complete = null;
					transTweenR.listener_complete = lisCompleteHandle;
					isAddListener = true;
				}
			}
			if(_s!=FV.un){
				transTweenW.fix(this,"scale",this.scale,_s,_time,_tweenType);	
				transTweenW.start();
				if(isAddListener==false){
					transTweenW.lisHalf = _lisHalf;
					transTweenR.listener_complete = null;
					transTweenW.listener_complete = lisCompleteHandle;
					isAddListener = true;
				}
			}
		}
	}
}